package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.ReservationMapper;
import com.cqjtu.drive.mapper.StudentMapper;
import com.cqjtu.drive.pojo.Reservation;
import com.cqjtu.drive.pojo.Student;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.ReservationService;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Map<String,String> getReservationById(String reserveId) {
        Reservation reservation = reservationMapper.getReservationById(reserveId);
        Map<String,String> resp = new HashMap<>();
        resp.put("reserveId",reservation.getReserveId());
        resp.put("sId",reservation.getsId());
        resp.put("coId",reservation.getCoId());
        resp.put("reserveAccept",reservation.getReserveAccept().toString());
        resp.put("reserveFinish",reservation.getReserveFinish().toString());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(reservation.getReserveTime());
        resp.put("reserveTime",date);
        return resp;
    }

    @Override
    public Response<String> insertReservation(Reservation reservation) {
        try {
            List<Reservation> list = reservationMapper.getAllReservations();
            if(list != null && !list.isEmpty()){
                String id = Integer.toString(Integer.parseInt(list.get(list.size() - 1).getReserveId()) + 1);
                reservation.setReserveId(id);
            }else {
                reservation.setReserveId("1");
            }
            long thm = reservation.getReserveTime().getTime();
            thm+=24*60*60*1000;
            reservation.setReserveTime(new Date(thm));
            long a= 16*24*60*60*1000;
            long chm=System.currentTimeMillis();
            if(thm<=chm+a||thm<chm){
                reservation.setReserveAccept(0);
                reservation.setReserveFinish(0);
                reservationMapper.insertReservation(reservation);
                return new Response<>(true, "Insert success", null);
            }else {
                return new Response<>(false, "预约时间须在今天到十五天以内", null);
            }
        } catch (Exception e) {
            return new Response<>(false, "Insert failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<String> updateReservation1(Reservation reservation) {
        try {
            reservationMapper.updateReservation1(reservation);
            return new Response<>(true, "Update success", null);
        } catch (Exception e) {
            return new Response<>(false, "Update failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<String> updateReservation2(Reservation reservation) {
        try {
            Student s=studentMapper.getStudentById(reservation.getsId());
            s.setsDay(s.getsDay()+1);
            System.out.println(s);
            studentMapper.updateStudent(s);
            reservationMapper.updateReservation2(reservation);
            return new Response<>(true, "Update success", null);
        } catch (Exception e) {
            return new Response<>(false, "Update failed: " + e.getMessage(), null);
        }
    }
    @Override
    public Response<String> deleteReservation(String reserveId) {
        try {
            reservationMapper.deleteReservation(reserveId);
            return new Response<>(true, "Delete success", null);
        } catch (Exception e) {
            return new Response<>(false, "Delete failed: " + e.getMessage(), null);
        }
    }

    @Override
    public List<Map<String,String>> getAllReservations() {
        List<Reservation> reservations = reservationMapper.getAllReservations();
        List<Map<String,String>> resp = new ArrayList<>();
        for (Reservation r : reservations){
            Map<String,String> map = new HashMap<>();
            map.put("reserveId",r.getReserveId());
            map.put("sId",r.getsId());
            map.put("coId",r.getCoId());
            map.put("reserveAccept",r.getReserveAccept().toString());
            map.put("reserveFinish",r.getReserveFinish().toString());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = dateFormat.format(r.getReserveTime());
            map.put("reserveTime",date);
            resp.add(map);
        }
        return resp;
    }

    @Override
    public List<Map<String, String>> getAllReservationsWithAccept0() {
        List<Reservation> reservations = reservationMapper.getAllReservationsWithaccept0();
        List<Map<String,String>> resp = new ArrayList<>();
        for (Reservation r : reservations){
            Map<String,String> map = new HashMap<>();
            map.put("reserveId",r.getReserveId());
            map.put("sId",r.getsId());
            map.put("coId",r.getCoId());
            map.put("reserveAccept",r.getReserveAccept().toString());
            map.put("reserveFinish",r.getReserveFinish().toString());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = dateFormat.format(r.getReserveTime());
            map.put("reserveTime",date);
            resp.add(map);
        }
        return resp;
    }

    @Override
    public List<Map<String, String>> getAllReservationsWithFinish1(String coId) {
        List<Reservation> reservations = reservationMapper.getAllReservationsWithfinish1(coId);
        List<Map<String,String>> resp = new ArrayList<>();
        for (Reservation r : reservations){
            Map<String,String> map = new HashMap<>();
            map.put("reserveId",r.getReserveId());
            map.put("sId",r.getsId());
            map.put("coId",r.getCoId());
            map.put("reserveAccept",r.getReserveAccept().toString());
            map.put("reserveFinish",r.getReserveFinish().toString());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = dateFormat.format(r.getReserveTime());
            map.put("reserveTime",date);
            resp.add(map);
        }
        return resp;
    }

    @Override
    public List<Map<String,String>> getReservationsByStudentId(String sId) {
        List<Reservation> reservations = reservationMapper.getReservationsByStudentId(sId);
        List<Map<String,String>> resp = new ArrayList<>();
        for (Reservation r : reservations){
            Map<String,String> map = new HashMap<>();
            map.put("reserveId",r.getReserveId());
            map.put("sId",r.getsId());
            map.put("coId",r.getCoId());
            map.put("reserveAccept",r.getReserveAccept().toString());
            map.put("reserveFinish",r.getReserveFinish().toString());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = dateFormat.format(r.getReserveTime());
            map.put("reserveTime",date);
            resp.add(map);
        }
        return resp;
    }

    @Override
    public List<Map<String, String>> getBycoId(String coId) {
        List<Map<String,String>> resp = new ArrayList<>();
        List<Reservation> reservations = reservationMapper.getBycoId(coId);
        for (Reservation r : reservations){
            Map<String,String> map = new HashMap<>();
            map.put("reserveId",r.getReserveId());
            map.put("sId",r.getsId());
            map.put("coId",r.getCoId());
            map.put("reserveAccept",r.getReserveAccept().toString());
            map.put("reserveFinish",r.getReserveFinish().toString());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = dateFormat.format(r.getReserveTime());
            map.put("reserveTime",date);
            resp.add(map);
        }
        return resp;
    }
}
